{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#### ICS 104 - Introduction to Programming in Python and C  \n",
    "# Functions\n",
    "## Reading Assignment\n",
    "- Chapter 5 Sections 1, 2, 3, 4, 5 and 8."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Chapter Learning Outcomes\n",
    "### At the end of this chapter, you will be able to"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- implement functions\n",
    "- become familiar with the concept of parameter passing\n",
    "- develop strategies for decomposing complex tasks into simpler ones\n",
    "- determine the scope of a variable"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Functions as Black Boxes\n",
    "- A <font color='blue'>**function**</font> is a sequence of instructions with a name."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- For example, the <font color='blue'>**round**</font> function, contains instructions to round a floating point value to a specified number of decimal places.\n",
    "- You <font color='blue'>**call**</font> a function in order to execute its instruction. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "price = round(6.8275,2) # Sets results to 6.83\n",
    "print(\"Price:\",price)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Functions as Black Boxes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- By using the expression <font color='blue'>**round(6.8275,2)**</font>, your program <font color='blue'>**calls**</font> the <font color='blue'>**round**</font> function, asking it to round 6.8275 to two decimal digits. \n",
    "- The instructions of the round function execute and compute the result. \n",
    "- The round function returns its result back to where the function was called and your program resumes execution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Functions as Black Boxes"
   ]
  },
  {
   "attachments": {
    "ch05fig1.PNG": {
     "image/png": "[encoded data, 41804 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![ch05fig1.PNG](attachment:ch05fig1.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Functions as Black Boxes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- When another function calls the round function, it provides <font color='blue'>**“inputs”**</font>, such as the values 6.8275 and 2 in the call round(6.8275, 2). \n",
    "- These values are called the <font color='blue'>**arguments**</font> of the function call. \n",
    "    - Note that they are not necessarily inputs provided by a human user. \n",
    "    - They are simply the values for which we want the function to compute a result."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The <font color='blue'>**“output”**</font> that the round function computes is called the <font color='blue'>**return value**</font>."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Functions as Black Boxes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Functions can receive multiple arguments, but they return only one value.\n",
    "- It is also possible to have functions with no arguments. \n",
    "- An example is the <font color='blue'>**random**</font> function that requires no argument to produce a random number"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- At this point, you may wonder how the round function performs its job.\n",
    "- How does round compute that 6.8275 rounded to two decimal digits is 6.83?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "price = round(6.8275,2)\n",
    "print(\"Price:\",price)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Functions as Black Boxes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Fortunately, as a user of the function, you do not need to know how the function is implemented."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- You just need to know the specification of the function:\n",
    "    - If you provide arguments x and n, the function returns x rounded to n decimal digits."
   ]
  },
  {
   "attachments": {
    "ch05fig2.PNG": {
     "image/png": "[encoded data, 17668 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![ch05fig2.PNG](attachment:ch05fig2.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- We can think of round as a black box. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Functions as Black Boxes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- When you design your own functions, you will want to make them appear as black boxes to other programmers.\n",
    "- Those programmers want to use your functions without knowing what goes on inside. \n",
    "- Even if you are the only person working on a program, making each function into a black box pays off: there are fewer details that you need to keep in mind."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Implementing and Testing Functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Implementing a Function\n",
    "- When defining a <font color='blue'>**function**</font>, you provide a <font color='blue'>**name**</font> for the function and a <font color='blue'>**variable**</font> for each <font color='blue'>**argument**</font>. "
   ]
  },
  {
   "attachments": {
    "ch05fig3.PNG": {
     "image/png": "[encoded data, 412724 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Let us start with a very simple example: \n",
    "    - a function to compute the volume of a cube with a given side length.\n",
    "![ch05fig3.PNG](attachment:ch05fig3.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Implementing and Testing Functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- When writing this function, you need to\n",
    "    - Pick a <font color='blue'>**name**</font> for the function (cubeVolume)\n",
    "    - Define a variable for each <font color='blue'>**argument**</font> (sideLength). These variables are called the <font color='blue'>**parameter variables**</font>.\n",
    "- Put all this information together along with the <font color='blue'>**def**</font> reserved word to form the first line of the function's definition:\n",
    "    - <font color='blue'>**def cubeVolume(sideLength):**</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "def cubeVolume(sideLength):\n",
    "    volume = sideLength ** 3\n",
    "    return volume"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- This line is called the <font color='blue'>**header**</font> of the function. \n",
    "- Next, specify the <font color='blue'>**body**</font> of the function.\n",
    "    - The body contains the statements that are executed when the function is called. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- In order to return the result of the function, use the <font color='blue'>**return**</font> statement:\n",
    "    - <font color='blue'>**return volume**</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Implementing and Testing Functions"
   ]
  },
  {
   "attachments": {
    "ch05fig4.PNG": {
     "image/png": "[encoded data, 92700 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![ch05fig4.PNG](attachment:ch05fig4.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Implementing and Testing Functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Testing a Function\n",
    "- In order to test the function, your program should contain:\n",
    "    - The definition of the function.\n",
    "    - Statements that call the function and print the result. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "def cubeVolume(sideLength):\n",
    "    volume = sideLength ** 3\n",
    "    return volume\n",
    "result1 = cubeVolume(2)\n",
    "result2 = cubeVolume(10)\n",
    "print(\"A cube with side length 2 has volume\", result1)\n",
    "print(\"A cube with side length 10 has volume\", result2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Implementing and Testing Functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Programs that Contain Functions\n",
    "- When you write a program that contains one or more functions, you need to pay attention to the order of the function definitions and statements in the program.\n",
    "- As the Python interpreter reads the source code, it reads each function definition and each statement.\n",
    "    - The statements in a function definition are not executed until the function is called.\n",
    "    - Any statement not in a function definition, on the other hand, is executed as it is encountered.\n",
    "- Therefore, it is important that you define each function before you call it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Implementing and Testing Functions"
   ]
  },
  {
   "attachments": {
    "ch05fig5.PNG": {
     "image/png": "[encoded data, 85732 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![ch05fig5.PNG](attachment:ch05fig5.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Implementing and Testing Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "#  This program computes the volumes of two cubes.\n",
    "\n",
    "def main() :\n",
    "   result1 = cubeVolume(2)\n",
    "   result2 = cubeVolume(10)\n",
    "   print(\"A cube with side length 2 has volume\", result1)\n",
    "   print(\"A cube with side length 10 has volume\", result2)\n",
    "   \n",
    "## Computes the volume of a cube.\n",
    "#  @param sideLength the length of a side of the cube\n",
    "#  @return the volume of the cube\n",
    "#\n",
    "def cubeVolume(sideLength) :\n",
    "   volume = sideLength ** 3\n",
    "   return volume\n",
    "   \n",
    "# Start the program.\n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Implementing and Testing Functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Student Activity\n",
    "- Define a function squareArea that computes the area of a square of a given side length. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "slideshow": {
     "slide_type": "notes"
    }
   },
   "outputs": [],
   "source": [
    "def squareArea (sideLen) :\n",
    "    area = sideLen ** 2\n",
    "    return area\n",
    "\n",
    "print (squareArea(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Parameter Passing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "- When a function is called, variables are created for receiving the function’s arguments.\n",
    "- These variables are called <font color='blue'>**parameter variables**</font>.\n",
    "    - (Another commonly used term is <font color='blue'>**formal parameters**</font>.)\n",
    "- The values that are supplied to the function when it is called are the <font color='blue'>**arguments**</font> of the call.\n",
    "    - (These values are also commonly called the <font color='blue'>**actual parameters**</font>.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Parameter Passing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "#  This program computes the volumes of two cubes.\n",
    "\n",
    "def main() :\n",
    "   result1 = cubeVolume(2)\n",
    "   result2 = cubeVolume(10)\n",
    "   print(\"A cube with side length 2 has volume\", result1)\n",
    "   print(\"A cube with side length 10 has volume\", result2)\n",
    "   \n",
    "## Computes the volume of a cube.\n",
    "#  @param sideLength the length of a side of the cube\n",
    "#  @return the volume of the cube\n",
    "#\n",
    "def cubeVolume(sideLength) :\n",
    "   volume = sideLength ** 3\n",
    "   return volume\n",
    "   \n",
    "# Start the program.\n",
    "main()"
   ]
  },
  {
   "attachments": {
    "ch05fig6.PNG": {
     "image/png": "[encoded data, 19876 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "![ch05fig6.PNG](attachment:ch05fig6.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "- The parameter variable <font color='blue'>**sideLength**</font> of the <font color='blue'>**cubeVolume**</font> function is created when the function is called."
   ]
  },
  {
   "attachments": {
    "ch05fig7.PNG": {
     "image/png": "[encoded data, 24000 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![ch05fig7.PNG](attachment:ch05fig7.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The parameter variable is initialized with the value of the argument that was passed in the call. In our case, <font color='blue'>**sideLength**</font> is set to 2."
   ]
  },
  {
   "attachments": {
    "ch05fig8.PNG": {
     "image/png": "[encoded data, 28460 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "![ch05fig8.PNG](attachment:ch05fig8.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "- The function computes the expression <font color='blue'>**sideLength ** 3**</font>, which has the value 8. \n",
    "- That value is stored in the variable <font color='blue'>**volume**</font>."
   ]
  },
  {
   "attachments": {
    "ch05fig9.PNG": {
     "image/png": "[encoded data, 15864 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![ch05fig9.PNG](attachment:ch05fig9.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "- The function returns. All of its variables are removed. \n",
    "- The return value is transferred to the <font color='blue'>**caller**</font>, that is, the function calling the <font color='blue'>**cubeVolume**</font> function. \n",
    "- The caller puts the return value in the <font color='blue'>**result1**</font> variable."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Parameter Passing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Student Activity\n",
    "- What does this program print? Use a diagram to find the answer. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "def main():\n",
    "    a = 5\n",
    "    b = 7\n",
    "    print(mystery(a,b))\n",
    "def mystery(x,y):\n",
    "    z = x + y \n",
    "    z = z / 2.0\n",
    "    return z\n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Return Values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The <font color='blue'>**return**</font> statement terminates a function call and yields the function result. \n",
    "- In the preceding examples, each <font color='blue'>**return**</font> statement returned a variable. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- However, the <font color='blue'>**return**</font> statement can return the value of any expression. \n",
    "- Instead of saving the return value in a variable and returning the variable, it is often possible to eliminate the variable and return the value of a more complex expression: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "def cubeVolume(sideLength):\n",
    "    return sideLength ** 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- When the <font color='blue'>**return**</font> statement is processed, the function exits <font color='blue'>**immediately**</font>."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Return Values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Some programmers find this behavior convenient for handling exceptional cases at the beginning of the function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "def cubeVolume(sideLength):\n",
    "    if sidelength < 0:\n",
    "        return 0\n",
    "    # Handle the regular case."
   ]
  },
  {
   "attachments": {
    "ch05fig10.PNG": {
     "image/png": "[encoded data, 25412 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![ch05fig10.PNG](attachment:ch05fig10.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Return Values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Some programmers dislike the use of multiple <font color='blue'>**return**</font> statements in a function. \n",
    "- You can avoid multiple returns by storing the function result in a variable that you return in the last statement of the function. \n",
    "- For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "def cubeVolume(sideLength):\n",
    "    if sideLength >= 0:\n",
    "        volume = sideLength ** 3\n",
    "    else:\n",
    "        volume = 0\n",
    "    return volume"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Return Values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Student Activity\n",
    "- What does this function do? "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "def mystery(n):\n",
    "    if n % 2 == 0:\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Functions Without Return Values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Some functions may not return a value, but they can produce output. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Sometimes, you need to carry out a sequence of instructions that does not yield a value.\n",
    "- If that instruction sequence occurs multiple times, you will want to package it into a function."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Functions Without Return Values"
   ]
  },
  {
   "attachments": {
    "ch05fig11.PNG": {
     "image/png": "iVBORw0KGgoAAAANSUhEUgAAAH0AAABeCAYAAAAOuBu9AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAATRSURBVHhe7Z3fa1tlGMf9TwohUHqhhFRxTCIiOIO6XZTN4cTVNatCKf6AOsHNC3+AzYUJSutFd9HuIruYFx3iClZYKlYoQmSdGrQXKVLWlnbUzTDTNen5ek7OOeE0OeckPcmxTZ7vB56Lvnny8ub59D3n5OWBPAIiDkoXCKULhNIFQukCoXSBULpAKF0glC4QShcIpQuE0gXSQul5ZBJRBLu6XGIY19eKRr5OMZNAr22uGSEMTq8Y2QbFDJIhu1xLxKaxZqTrdOb6vNBC6Q+QTb2PwVjMJT5HerNk5OuUsikM2+aaMYR4et3INihlcXXILtcSo2ncNdJ1OnN9XuDlXSCULhBKFwilC4TSBULpAqF0gVC6QChdIJQuEEoXCKULhNIFQukCoXSBULpAKF0glC4QOdLXpjFo23dm0+NWYQXXYyGb96hR0+PWPvgmvdJQ6FYcU0QogYxdv1+lwbAFDYGbacT39Ju9jpORHnVuN+nrSI8OWd4Tw/m+CLr/B+kN1c8jcqTXYHafukmvxU8ZViid0lsKpVN666D05mhL6aXFMTytLrp7+AY2jLEampSuFO4gM/0FLpyNlgvUHTmF4Y+mMJf7B4qR44zf0kvI535AavRdnC4/MAbQG43hw/FvsLixbeQ401D9POKbdFNobyIDxz3ahHQlfxtX3nhKfU2T3YfzsQG8Fg2X/w6Gh3Dl93t1xPspfRurNz/DSwFt7ZrsM5ZvCw2ur5H6eaQ9pSsbmP/4BXU8jP7xn7Ba2NXHi3dxa2IAPeqc3adTyBnD9vgnXVn9Fu+EA+rcz+PCtV+xVdT1KoW/8GOyX1/f8a+w+K+L9o6XXjf2St/NpfCKtouOX0Z2p6pwDxYQP6IV/Byu5twuo35Jz+P2WJ86bwBPXrqJrWqvO1lMvfyY+voRvH3jjvNu73jpgQhODlgPTowY6MMz5UukVfo2llPn1LEgohO/qXfOasxTtOcQX7hvjNnhk/TdPzB1QruMP4tP5reMQSvm+rvw6MU0HFfY8dL3dXnfRPpiRB0z75XV/yxn8GJI2+n1ZPok/X4al3q0Nb+Jr1d2jEErCrYX4ghrc5yYxJLTLagtpW/N4r2g02408CTd5Tx8TxyQdPMzVT+HWKjM4fS5NRqpn0f8k14WVqegnqSv4/uRo/uWVcsh3+mN1M8j/klvBE/SC1iafFUdC+LY2C08NEb3zyG/p/tIG0pX8HBxHMe09z3+AWZW6x902OOTdHXeljy9+4hP0gtYnp1AMpFA8stZLDvdlDxJV1FWMPOW/jD3xNkkvstuoGCtXjGPtT9/xnzWbqeZ+CVdXV7T39MbrJ9HfJJuFtRFqIZX6SrK3xlcjukncnuf5A/46b1MsydyDdbPI20rXUM7e/9lZhLxkX5DtDZXFP0jn2Li2hyymwdxOGOyg3tLcx7P3ttSOjnMULpAKF0glC4QShcIpQuE0gVC6QKhdIFQukAoXSCULhBKFwilC4TSBULpAqF0gVC6QChdIJQuEEoXCKULhNIF0kLpll5tx6jtX6/0kTuGTV96pR/eJWr60jtzfV5ooXT+qnIF/qoyOWxQukAoXSCULhBKFwilC4TSBULpAqF0gVC6QChdIJQuEEoXCKULhNIFQukCoXSBULo4gP8ARsVJQDWpKW8AAAAASUVORK5CYII="
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Here is a typical example: Your task is to print a string in a box, like this:\n",
    "![ch05fig11.PNG](attachment:ch05fig11.PNG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "## Prints a string in a box.\n",
    "# @param contents the string to enclose in a box\n",
    "#\n",
    "def boxString(contents):\n",
    "    n = len(contents)\n",
    "    if n == 0:\n",
    "        return #Return immediately\n",
    "    print(\"-\"*(n+2))\n",
    "    print(\"!\"+contents+\"!\")\n",
    "    print(\"-\"*(n+2))\n",
    "\n",
    "def main():\n",
    "    boxString(\"Hello\")\n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Student Activity\n",
    "- What is wrong with the following statement?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "print(boxString(\"Hello\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Variable Scope"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- As your programs get larger and contain more variables, you may encounter problems where you cannot access a variable that is defined in a different part of your program, or where two variable definitions conflict with each other. \n",
    "- In order to resolve these problems, you need to be familiar with the concept of variable scope."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The <font color='blue'>**scope**</font> of a variable is the part of the program in which you can access it. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Variable Scope"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- In the following code segment, the <font color='blue'>**scope**</font> of the parameter variable <font color='blue'>**sideLength**</font> is the entire <font color='blue'>**cubeVolume**</font> function but <font color='blue'>**not**</font> the <font color='blue'>**main**</font> function."
   ]
  },
  {
   "attachments": {
    "ch05fig12.PNG": {
     "image/png": "[encoded data, 18644 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![ch05fig12.PNG](attachment:ch05fig12.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Variable Scope"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- A variable that is defined within a function is called a <font color='blue'>**local variable**</font>. \n",
    "- When a local variable is defined in a block, it becomes available from that point until the end of the function in which it is defined. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- For example, in the code segment below, the scope of the square variable is highlighted. "
   ]
  },
  {
   "attachments": {
    "ch05fig13.PNG": {
     "image/png": "[encoded data, 18736 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![ch05fig13.PNG](attachment:ch05fig13.PNG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "def main() :\n",
    "    sum = 0\n",
    "    for i in range(11) :\n",
    "        square = i * i\n",
    "        sum = sum + square\n",
    "    print(square,sum)\n",
    "    \n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Variable Scope"
   ]
  },
  {
   "attachments": {
    "ch05fig14.PNG": {
     "image/png": "[encoded data, 28228 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![ch05fig14.PNG](attachment:ch05fig14.PNG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "def main() :\n",
    "    sideLength = 10\n",
    "    result = cubeVolume()\n",
    "    print(result)\n",
    "    \n",
    "def cubeVolume() :\n",
    "    return sideLength **3\n",
    "\n",
    "main() "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Note the scope of the variable <font color='blue'>**sideLength**</font>. \n",
    "- The <font color='blue'>**cubeVolume**</font> function attempts to read the variable, but it cannot;\n",
    "    - The scope of <font color='blue'>**sideLength**</font> does not extend outside the <font color='blue'>**main**</font> function."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Variable Scope"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- It is possible to use the variable name more than once in a program. \n",
    "- For example,"
   ]
  },
  {
   "attachments": {
    "ch05fig15.PNG": {
     "image/png": "[encoded data, 25288 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![ch05fig15.PNG](attachment:ch05fig15.PNG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "def main() :\n",
    "    result = square(3) + square(4)\n",
    "    print(result)\n",
    "    \n",
    "def square(n) :\n",
    "    result = n * n\n",
    "    return result\n",
    "\n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "- Each <font color='blue'>**result**</font> variable is defined in a separate function, and their scope do not overlap. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Variable Scope"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Any variable that is defined outside a function is a <font color='blue'>**global variable**</font>.\n",
    "- A <font color='blue'>**global variable**</font> is **visible** to all functions defined after it.\n",
    "    - i.e., you can get the value of the variable.\n",
    "- However, any function that wishes to update a <font color='blue'>**global variable**</font> must include a <font color='blue'>**global**</font> declaration:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "balance = 1000 # A global varaible\n",
    "\n",
    "def withdraw(amount) :\n",
    "#    global balance # This function intends to update the global balance variable\n",
    "    if balance >= amount:\n",
    "        balance = balance - amount\n",
    "    \n",
    "withdraw(200)\n",
    "print(balance)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "balance = 1000 # A global varaible\n",
    "\n",
    "def withdraw(amount) :\n",
    "#    global balance # This function intends to update the global balance variable\n",
    "    if balance >= amount:\n",
    "        newBalance = balance - amount\n",
    "    print(\"New Balance =\", newBalance)\n",
    "    \n",
    "withdraw(200)\n",
    "print(\"Current value of balance =\", balance)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- If you omit the <font color='blue'>**global**</font> declaration, then the balance variable inside the <font color='blue'>**withdraw**</font> function is considered a local variable."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Generally, global variables are not a good idea.\n",
    "- When multiple functions update global variables, the result can be difficult to predict. \n",
    "- Particularly in larger programs developed by multiple programmers, it is important that the effect of each function be clear and easy to understand. \n",
    "- You should avoid global variables in your programs. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Summary\n",
    "- A function is a named sequence of instructions.\n",
    "- Arguments are supplied when a function is called.\n",
    "- The return value is the result that the function computes.\n",
    "- When declaring a function, you provide a name for the function and a variable for each argument. \n",
    "- Function comments explain the purpose of the function, the meaning of the parameters and return values, as well as any special requirements. \n",
    "- Parameter variables hold the arguments supplied in the function call. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Summary\n",
    "- The return statement terminates a function call and yields the function result. \n",
    "    - Complete computations that can be reused into functions. \n",
    "- Use the process of stepwise refinement to decompose complex tasks into simpler ones.\n",
    "    - When you discover that you need a function, write a description of the parameter variables and return values.\n",
    "    - A function may require simpler functions to carry out its work. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Summary\n",
    "- The scope of a variable is the part of the program in which the variable is visible. \n",
    "    - Two local or parameter variables can have the same name, provided that their scope do not overlap. \n",
    "    - You can use the same variable name within different functions since their scope does not overlap. \n",
    "    - Local variable declared inside a function are not visible to code inside other functions. "
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "rise": {
   "theme": "yellow"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
